package com.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.enums.error.ResultEnum;
import com.common.enums.sys.DelFlagEnum;
import com.common.enums.user.LoginStatusEnum;
import com.common.enums.user.PasswordLevelEnum;
import com.common.enums.user.UserStatusEnum;
import com.common.exception.StefanieException;
import com.common.util.PasswordLevelUtil;
import com.common.util.StringUtil;
import com.user.config.StefanieParamConfig;
import com.user.domain.dto.UserCreateDto;
import com.user.domain.po.UserPo;
import com.user.domain.vo.UserVo;
import com.user.fegin.FileService;
import com.user.mapper.UserMapper;
import com.user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import static com.common.constant.DefaultSystemConstant.USER_PREFIX;


/**
 * @author: stefanie-zy
 * @time: 2024/1/25-14:09
 * @description: 用户接口实现层
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPo> implements UserService {

    private final UserMapper userMapper;
    private final FileService fileService;

    @Override
    public UserVo create(UserCreateDto userCreateDto) {
        UserVo userVo = new UserVo();
        UserPo userPo = new UserPo();
        BeanUtils.copyProperties(userCreateDto, userPo);
        // 密码、密码等级
        if (StringUtil.isNotEmpty(userPo.getPassword())) {
            userPo.setPasswordLevel(PasswordLevelUtil.evaluatePasswordStrength(userPo.getPassword()));
        //    userPo.setPassword(SecurityUtil.encryptPassword(userPo.getPassword()));
        } else {
            userPo.setPassword(StefanieParamConfig.defaultPassword);
            userPo.setPasswordLevel(PasswordLevelEnum.ZERO);
        }
        // 昵称设置随机名称
        if (StringUtil.isEmpty(userPo.getNickName())) {
            userPo.setNickName(USER_PREFIX + RandomStringUtils.random(8));
        }
        userPo.setLoginStatus(LoginStatusEnum.LOG_OUT);
        userPo.setStatus(UserStatusEnum.NORMAL);
        userPo.setDelFlag(DelFlagEnum.NOT_DELETE);
        // userPo.setIcoUrl(String.valueOf(fileService.getUserIcoUrl(userPo.getUserName()).getData()));
        userMapper.insert(userPo);
        // TODO 返回Vo信息
        return userVo;
    }

    @Override
    public void duplicationCheck(UserCreateDto userCreateDto) {
        QueryWrapper<UserPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name", userCreateDto.getLoginName());
        Long loginNameCount = userMapper.selectCount(queryWrapper);
        if (loginNameCount > 0) {
            throw new StefanieException(ResultEnum.B_DUPLICATION_DATA.getCode(), "login name duplication");
        }
        queryWrapper.clear();
        queryWrapper.eq("user_name", userCreateDto.getUserName());
        Long userNameCount = userMapper.selectCount(queryWrapper);
        if (userNameCount > 0) {
            throw new StefanieException(ResultEnum.B_DUPLICATION_DATA.getCode(), "user name duplication");
        }
        queryWrapper.clear();
        queryWrapper.eq("id_card", userCreateDto.getIdCard());
        Long idCardCount = userMapper.selectCount(queryWrapper);
        if (idCardCount > 0) {
            throw new StefanieException(ResultEnum.B_DUPLICATION_DATA.getCode(), "id card duplication");
        }
    }

}
